import type { UserInfo } from '@/interface/business'
import { defineStore } from 'pinia'
import { store } from '@/store'
import { removeToken } from '@/utils'

interface AuthState {
  token: string
  userInfo: UserInfo
}

// authStore 是一个函数，用于获取对象实例(单例)
const authStore = defineStore({
  // 区分不同状态的唯一标识
  id: 'auth-store',
  // 状态
  state: (): AuthState => {
    return {
      token: '',
      userInfo: {
        userId: '',
        userName: 'huabin',
        userPhone: '',
      },
    }
  },
  getters: {
    // 是否为登录状态
    isLogin: (state) => Boolean(state.token),
    upperToken: (state) => state.token.toUpperCase(),
  },
  actions: {
    // 重置 auth 状态
    resetAuthState() {
      // 将 state 中的数据重置为原始状态
      removeToken()
      this.$reset()
    },
    setToken(token: string) {
      this.token = token
    },
  },
})

export default function useAuthStore() {
  return authStore(store)
}
